package com.acme.benchmarks;

import com.acme.HashMapFactory;

import java.util.Map;

public class PutTimeBenchmark extends Benchmark {

    String accString = "";

    public PutTimeBenchmark(int iterations, int growFactor, HashMapFactory factory) {
        super(iterations, growFactor, factory);
    }

    @Override
    protected void runIteration(int iterationSize) {

        int addAmount = iterationSize / 20;


        long[] times = new long[5];

        for(int it = 0; it < times.length;it++){
            Map<Integer, Integer> hashMap =  generateMap(iterationSize);

            int startSize = hashMap.size();

            Stopwatch sw = new Stopwatch();
            sw.start();

            for (int i = 0; i < addAmount; i++) {
                hashMap.put(startSize + i, startSize + i);
            }

            times[it] = sw.getElapsed()  / addAmount;
            accString += ("" + hashMap.size()).substring(0, 1);
        }

        long maxValue = times[0];
        long minValue = times[0];
        long total = 0;

        for (long cur : times) {
            if (maxValue < cur) {
                maxValue = cur;
            }
            if (minValue > cur) {
                minValue = cur;
            }
            total += cur;
        }



        log(iterationSize + ":" + maxValue+":"+total/times.length+":"+minValue);

    }

    private static class Stopwatch {

        private long time;

        public void start() {
            time = System.nanoTime();
        }

        public long getElapsed() {
            return System.nanoTime() - time;
        }

    }

}
